package com.liqi.monitor.service.impl;

import com.liqi.common.annotation.DataScope;
import com.liqi.common.utils.DateUtils;
import com.liqi.common.utils.StringUtils;
import com.liqi.deployment.domain.DvDeployment;
import com.liqi.deployment.mapper.DvDeploymentMapper;
import com.liqi.monitor.domain.DvDeploymentSystem;
import com.liqi.monitor.domain.DvFaultLog;
import com.liqi.monitor.mapper.DvFaultLogMapper;
import com.liqi.monitor.service.IDvFaultLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 故障日志管理Service业务层处理
 *
 * @author Lwy
 * @date 2022-02-16
 */
@Service
public class DvFaultLogServiceImpl implements IDvFaultLogService {
	@Autowired
	private DvFaultLogMapper dvFaultLogMapper;
	@Autowired
	private DvDeploymentMapper dvDeploymentMapper;

	/**
	 * 查询故障日志管理
	 *
	 * @param id 故障日志管理主键
	 * @return 故障日志管理
	 */
//    @DataScope(deptAlias = "d" , userAlias = "u")
	@Override
	public DvFaultLog selectDvFaultLogById(Long id) {
		return dvFaultLogMapper.selectDvFaultLogById(id);
	}

	/**
	 * 查询故障日志管理列表
	 *
	 * @param dvFaultLog 故障日志管理
	 * @return 故障日志管理
	 */
	@Override
	@DataScope(deptAlias = "d", userAlias = "u", regionAlias = "r", groupAlias = "g")
	public List<DvFaultLog> selectDvFaultLogList(DvFaultLog dvFaultLog) {
		return dvFaultLogMapper.selectDvFaultLogList(dvFaultLog);
	}

	/**
	 * 新增故障日志管理
	 *
	 * @param dvFaultLog 故障日志管理
	 * @return 结果
	 */
	@Override
	public int insertDvFaultLog(DvFaultLog dvFaultLog) {
		dvFaultLog.setCreateTime(DateUtils.getNowDate());
		dvFaultLog.setStatus(0);
		return dvFaultLogMapper.insertDvFaultLog(dvFaultLog);
	}

	/**
	 * 修改故障日志管理
	 *
	 * @param dvFaultLog 故障日志管理
	 * @return 结果
	 */
	@Override
	public int updateDvFaultLog(DvFaultLog dvFaultLog) {
		dvFaultLog.setUpdateTime(DateUtils.getNowDate());
		return dvFaultLogMapper.updateDvFaultLog(dvFaultLog);
	}

	/**
	 * 批量删除故障日志管理
	 *
	 * @param ids 需要删除的故障日志管理主键
	 * @return 结果
	 */
	@Override
	public int deleteDvEquipmentFaultLogByIds(Long[] ids) {
		return dvFaultLogMapper.deleteDvEquipmentFaultLogByIds(ids);
	}

	/**
	 * 删除故障日志管理信息
	 *
	 * @param id 故障日志管理主键
	 * @return 结果
	 */
	@Override
	public int deleteDvEquipmentFaultLogById(Long id) {
		return dvFaultLogMapper.deleteDvEquipmentFaultLogById(id);
	}

	/**
	 * 批量逻辑删除故障日志管理信息
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public int tombstoneFaultLogByIds(Long[] ids) {
		return dvFaultLogMapper.tombstoneFaultLogByIds(ids);
	}

	@Override
	public void insertDvDeployDeviceModel(DvFaultLog dvFaultLog) {

	}

	@Override
	public DvFaultLog selectDvFaultLogByDeviceId(DvFaultLog dvFaultLog) {
		return dvFaultLogMapper.selectDvFaultLogByDeviceId(dvFaultLog);
	}

	/**
	 * 新增故障日志管理
	 *
	 * @param dvFaultLog 故障日志管理
	 * @return 结果
	 */
	@Override
	public void insertNonmanualFaultLog(DvFaultLog dvFaultLog) {
		DvDeployment dvDeployment = dvDeploymentMapper.selectDvDeploymentByDeviceId(dvFaultLog.getDeviceId());
		dvFaultLog.setDeptId(dvDeployment.getDeptId());
		dvFaultLog.setCreateBy("admin");
		String errorCode = dvFaultLog.getErrorCode();
		DvFaultLog log = dvFaultLogMapper.selectDvFaultLogByDeviceId(dvFaultLog);
		// 获取今天0:00:00的时间
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date zero = calendar.getTime();
		if (log == null) {
			dvFaultLog.setCreateTime(DateUtils.getNowDate());
			dvFaultLog.setStatus(0);
			this.insertDvFaultLog(dvFaultLog);
			// 如果设备出现异常，根据异常deviceId、异常码(由中间件定义)、今天时间、异常类型(0运行中上报 1人工上报
			// 2开机自检上报)作为与条件判定为唯一标记，那么每台设备通过心跳增加的异常信息，通过心跳、自检、运行过程中，发现的异常每天只允许发一条到异常表
		} else if (StringUtils.isNotBlank(log.getDeviceId())
				&& (log.getEquipmentFalutType().equals("2") || log.getEquipmentFalutType().equals("0"))
				&& (log.getCreateTime().before(zero))||!errorCode.equals(log.getErrorCode())) {
			dvFaultLog.setCreateTime(DateUtils.getNowDate());
			dvFaultLog.setStatus(0);
			this.insertDvFaultLog(dvFaultLog);

		}
		return;
	}

	/**
	 * 阈值校验
	 *
	 * @param
	 * @return
	 */
	@Override
	public void insertThresholdFaultLog(DvDeploymentSystem dvDeploymentSystem) {

		//70的阈值
		String heightIsDanger = this.selectHeightIsDangerByDictValue();
		Integer threshold = null;
		if (StringUtils.isNotBlank(heightIsDanger)) {
			threshold = Integer.parseInt(heightIsDanger);
		}
		//30的阈值
		String lowIsDanger = this.selectLowIsDangerByDictValue();
		Integer printWarn = null;
		if (StringUtils.isNotBlank(lowIsDanger)) {
			printWarn = Integer.parseInt(lowIsDanger);
		}
		// 内存使用率达到70%
		Integer memoryRate = null;
		if (StringUtils.isNotBlank(dvDeploymentSystem.getMemoryRate())) {
			memoryRate = Integer.parseInt(dvDeploymentSystem.getMemoryRate());		
		if (memoryRate >= threshold) {
			DvFaultLog log = new DvFaultLog();
			log.setDeviceId(dvDeploymentSystem.getDeviceId());
			log.setEquipmentFalutType("0");
			// 超过设定阈值异常上报
			log.setErrorCode("1000");
			log.setErrorMessage("内存使用率已超"+ threshold);
			this.insertNonmanualFaultLog(log);
			}
		}
		// 磁盘使用率达到70%
		Integer diskRate = null;
		if (StringUtils.isNotBlank(dvDeploymentSystem.getDiskRate())) {
			diskRate = Integer.parseInt(dvDeploymentSystem.getDiskRate());		
		if (diskRate >= threshold) {
			DvFaultLog log = new DvFaultLog();
			log.setDeviceId(dvDeploymentSystem.getDeviceId());
			log.setEquipmentFalutType("0");
			// 超过设定阈值异常上报
			log.setErrorCode("2000");
			log.setErrorMessage("磁盘使用率已超"+ threshold);
			this.insertNonmanualFaultLog(log);
			}
		}
		// 纸张剩余量30
		Integer paperRate = null;
		if (StringUtils.isNotBlank(dvDeploymentSystem.getPaperRate())) {
			paperRate = Integer.parseInt(dvDeploymentSystem.getPaperRate());		
		if (paperRate >= printWarn) {
			DvFaultLog log = new DvFaultLog();
			log.setDeviceId(dvDeploymentSystem.getDeviceId());
			log.setEquipmentFalutType("0");
			// 超过设定阈值异常上报
			log.setErrorCode("3000");
			log.setErrorMessage("纸张剩余不足"+ printWarn);
			this.insertNonmanualFaultLog(log);
			}
		}
		// 青色墨盒使用率达到70%
		Integer cColor = null;
		if (StringUtils.isNotBlank(dvDeploymentSystem.getCColor())) {
			cColor = Integer.parseInt(dvDeploymentSystem.getCColor());		
		if (cColor >= printWarn) {
			DvFaultLog log = new DvFaultLog();
			log.setDeviceId(dvDeploymentSystem.getDeviceId());
			log.setEquipmentFalutType("0");
			// 超过设定阈值异常上报
			log.setErrorCode("4000");
			log.setErrorMessage("青色墨盒剩余不足"+ printWarn);
			this.insertNonmanualFaultLog(log);
			}
		}
		// 品红色墨盒使用率达到70%
		Integer mColor = null;
		if (StringUtils.isNotBlank(dvDeploymentSystem.getMColor())) {
			mColor = Integer.parseInt(dvDeploymentSystem.getMColor());		
		if (mColor >= printWarn) {
			DvFaultLog log = new DvFaultLog();
			log.setDeviceId(dvDeploymentSystem.getDeviceId());
			log.setEquipmentFalutType("0");
			// 超过设定阈值异常上报
			log.setErrorCode("5000");
			log.setErrorMessage("品红色墨盒剩余不足"+ printWarn);
			this.insertNonmanualFaultLog(log);
			}
		}
		// 黄色墨盒使用率达到70%
		Integer yColor = null;
		if (StringUtils.isNotBlank(dvDeploymentSystem.getYColor())) {
			yColor = Integer.parseInt(dvDeploymentSystem.getYColor());		
		if (yColor >= printWarn) {
			DvFaultLog log = new DvFaultLog();
			log.setDeviceId(dvDeploymentSystem.getDeviceId());
			log.setEquipmentFalutType("0");
			// 超过设定阈值异常上报
			log.setErrorCode("6000");
			log.setErrorMessage("黄色墨盒剩余不足"+ printWarn);
			this.insertNonmanualFaultLog(log);
			}
		}
		// 黑色墨盒使用率达到70%
		Integer kColor = null;
		if (StringUtils.isNotBlank(dvDeploymentSystem.getKColor())) {
			kColor = Integer.parseInt(dvDeploymentSystem.getKColor());		
		if (kColor >= printWarn) {
			DvFaultLog log = new DvFaultLog();
			log.setDeviceId(dvDeploymentSystem.getDeviceId());
			log.setEquipmentFalutType("0");
			// 超过设定阈值异常上报
			log.setErrorCode("7000");
			log.setErrorMessage("黑色墨盒剩余不足"+ printWarn);
			this.insertNonmanualFaultLog(log);
			}
		}

	}
	@Override
	public String selectHeightIsDangerByDictValue() {
		return dvFaultLogMapper.selectHeightIsDangerByDictValue();
	}
	@Override
	public String selectLowIsDangerByDictValue() {
		return dvFaultLogMapper.selectLowIsDangerByDictValue();
	}
}
